﻿using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.Services.Clab.UI.Mvvmc;
using Microsoft.Services.Clab.UI.Mvvmc.Composition;
using RicSal.BusinessLogic.NationalIdNumber;
using RicSal.BusinessLogic.NationalIdNumber.Providers;

namespace Mari.SmartHotel.Modules.Foundation.Guests
{
    [ControllerExport("Modules.Foundation.Guests.EditGuest")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class EditGuestController
    {
        #region ViewModel reference
        public new EditGuestViewModel ViewModel { get { return base.ViewModel as EditGuestViewModel; } }
        #endregion

        #region Controller Initialization
        protected override IViewModel CreateViewModel()
        {
            return new EditGuestViewModel();
        }
        #endregion

        private void InitializeList()
        {
            this.ViewModel.Countries = Container.Countries;
            this.ViewModel.Companies = Container.Companies;
            this.ViewModel.Genders = Container.Genders;
            this.ViewModel.IdentificationTypes = Container.IdentificationTypes;
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();

            if (this.InitParameter is Data.Guest)
            {
                this.ViewModel.Guest = this.InitParameter as Data.Guest;
                this.Container.Attach(this.ViewModel.Guest);
            }
            else
            {
                this.ViewModel.Guest = Container.CreateObject<Data.Guest>();
                Container.AddToGuests(this.ViewModel.Guest);
            }

            InitializeList();

            this.ViewModel.IsModify = this.ViewModel.Guest.EntityKey != null;

            if (this.ViewModel.Guest.Identifications.Count == 0)
            {
                this.ViewModel.CurrentIdentification = Container.CreateObject<Data.Identification>();
                this.ViewModel.Guest.Identifications.Add(this.ViewModel.CurrentIdentification);
            }
            else
            {
                this.ViewModel.CurrentIdentification = this.ViewModel.Guest.Identifications.FirstOrDefault();
            }

            CheckPrivacyList();
        }

        protected override void OnPropertyChanged(string propertyName)
        {
            base.OnPropertyChanged(propertyName);
        }

        private void CheckPrivacyList()
        {
            var privacyList = Container.PrivacyList;

            if (this.ViewModel.Guest.PrivacyList.Count == 0)
            {
                foreach (var item in privacyList)
                {
                    this.ViewModel.Guest.PrivacyList.Add(
                        new Data.GuestPrivacyList
                        {
                            Guest = this.ViewModel.Guest,
                            Privacy = item,
                            Value = null
                        });
                }
            }
            else
            {
                var newPrivacyItems = Container.GetNullPrivacyListForGuest(this.ViewModel.Guest.GuestId);

                foreach (var item in newPrivacyItems)
                {
                    this.ViewModel.Guest.PrivacyList.Add(
                        new Data.GuestPrivacyList
                        {
                            Guest = this.ViewModel.Guest,
                            Privacy = item,
                            Value = null
                        });
                }
            }
        }

        partial void AddIdentificationExecuted()
        {
            this.ViewModel.CurrentIdentification = Container.CreateObject<Data.Identification>();
            this.ViewModel.Guest.Identifications.Add(this.ViewModel.CurrentIdentification);
        }

        partial void DeleteIdentificationExecuted()
        {
            this.Container.DeleteObject(this.ViewModel.CurrentIdentification);
            this.ViewModel.CurrentIdentification = this.ViewModel.Guest.Identifications.FirstOrDefault();
        }

        partial void SaveGuestExecuted()
        {
            Container.SaveChanges();
            this.ViewModel.IsModify = true;
        }

        partial void GenerateFiscalCodeExecuted()
        {
            var fiscalCode = NationalIdNumberFactory.Create<ItalianFiscalCode>().Generate(new PhysicalPerson()
            {
                FirstName = this.ViewModel.Guest.Name,
                LastName = this.ViewModel.Guest.Surname,
                Gender = this.ViewModel.Guest.Gender.GenderCode == "M" ? PhysicalPersonGender.Male : PhysicalPersonGender.Female,
                BirthDate = this.ViewModel.Guest.DateOfBirth,
                BirthCityCode = this.ViewModel.Guest.BirthCity.FinanceCode
            })[0];

            this.ViewModel.Guest.CF = fiscalCode;
        }
    }
}
